package com.wzq.service.impl;

import com.wzq.controller.admin.GetSome;
import com.wzq.dao.dto.ProjectAcceptdto;
import com.wzq.dao.dto.ProjectDto;
import com.wzq.dao.pojo.Image;
import com.wzq.dao.pojo.Project;
import com.wzq.mapper.Imagemapper;
import com.wzq.mapper.Projectmapper;
import com.wzq.service.IProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzq.util.AnalysisFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<Projectmapper, Project> implements IProjectService {
    @Autowired
    private Projectmapper projectmapper;
    @Autowired
    private Imagemapper imagemapper;
    public Long fileName;
    @Override
    public List<Project> getByType(String type) {
        return projectmapper.getByType(type);
    }

    @Override
    public int updateLove(int a, int id) {
        return projectmapper.updateLove(a,id);
    }

    @Override
    public List<Project> SelectResources() {
        return projectmapper.SelectResources();
    }

    @Override
    public List<Project> SelectActivity() {
        return projectmapper.SelectActivity();
    }

    @Override
    public List<Project> SelectActivityLike(String title) {
        return projectmapper.SelectActivityLike(title);
    }

    @Override
    public List<Project> getVidById(int id) {
        return projectmapper.getVidById(id);
    }

    @Override
    public List<ProjectDto> getResourcesBySpaceNameAndUserId(String space_name, Integer w_id) {
        return projectmapper.getResourcesBySpaceNameAndUserId(space_name,w_id);
    }

    @Override
    public List<Project> getResourcesByTitle(String title) {
        return projectmapper.getResourcesByTitle(title);
    }

    //搜索功能：猜你需要（随机查三条资源[设备]）
    @Override
    public List<Project> getResourcesRan() {
        return projectmapper.getResourcesRan();
    }

    @Override
    public List<Project> getResourcesRanFive() {
        return projectmapper.getResourcesRanFive();
    }

    //搜索功能：按名字条件查询所有活动
    @Override
    public List<Project> getActivityByTitle(String title) {
        return projectmapper.getActivityByTitle(title);
    }

    //搜索功能：猜你需要随机查三条活动
    @Override
    public List<Project> getActivityRan() {
        return projectmapper.getActivityRan();
    }

    //按类型ID查询该类型下的所有项目
    @Override
    public List<Project> GetProjectByType(Integer id) {
        return projectmapper.GetProjectByType(id);
    }

    @Override
    public void acceptImag(ProjectAcceptdto acceptdto,Integer p_id) throws IOException {
        ArrayList<Map<String, Object>> img1 = acceptdto.getImg1();
        ArrayList<Map<String, Object>> img2 = acceptdto.getImg2();
        ArrayList<Map<String, Object>> img3 = acceptdto.getImg3();
        mapforImage(img2,1,p_id);
        mapforImage(img1,2,p_id);
        mapforImage(img3,4,p_id);
    }

    private void mapforImage(ArrayList<Map<String, Object>> img, Integer type, Integer p_id) throws IOException {
        if(img.size()!=0){
            // 保存图片到 t_image
            Image image = new Image();
            image.setP_id(p_id);
            image.setType(type.byteValue());
            // 服务器静态路径

            // 文件保存路径
            String path="D:/upload/image/";
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            // 存放 base64 map
            String base64;
            Map<String, Object> analysis;
            // 图片二进制
            byte[] bytes;
            for (Map<String, Object> map : img) {
                if (map==null){
                    break;
                }
                base64 = String.valueOf(map.get("base64"));
                analysis = AnalysisFile.analysisImage(base64);
                bytes = (byte[]) analysis.get("imagData");
                String imagType = String.valueOf(analysis.get("imagType"));
                fileName = System.currentTimeMillis();
                String pathFile = path + fileName + imagType;
                File file = new File(pathFile);
                FileOutputStream fo = new FileOutputStream(file);
                fo.write(bytes);
                fo.flush();
                fo.close();
                System.out.println("图片上传成功");
                image.setUrl(fileName+imagType);
                imagemapper.insert(image);
            }
        }

    }

    @Override
    public void acceptVideo(ProjectAcceptdto acceptdto, Integer p_id) throws IOException {
        ArrayList<Map<String, Object>> video = acceptdto.getVideo();
        if (video.size()!=0){
            // 保存图片到 t_image
            Image image = new Image();
            image.setP_id(p_id);
            image.setType((byte) 5);
            // 服务器静态路径
//            String staticPath = "";
            // 文件保存路径
            String path="D:/upload/video/";
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            for (Map<String, Object> map : video) {
                if (map==null || "".equals(map.get("base64"))){
                    break;
                }
                //  提取base64数据
                String base64 = String.valueOf(map.get("base64"));
                Map<String, Object> analysis = AnalysisFile.analysisVideo(base64);
                byte[] bytes = (byte[]) analysis.get("videoData");
                //  提取文件后缀
                String videoType = String.valueOf(analysis.get("videoType"));
                // 封装文件名称
                fileName = System.currentTimeMillis();
                String pathFile =path+ fileName +videoType;
                // 保存文件到本地
                File file = new File(pathFile);
                FileOutputStream fo = new FileOutputStream(file);
                fo.write(bytes);
                fo.flush();
                fo.close();
                System.out.println("视频上传成功");
                // 将路径存到 t_image
                image.setUrl(fileName+videoType);
                imagemapper.insert(image);
            }
        }
    }
}
